home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / morse / staton / adpcm2.c next >
C/C++ Source or Header  |  1994-04-21  |  21KB  |  828 lines

  1. /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  2. /*  Copyright (C) 1994 Ken Staton  */
  3. /*      All Rights Reserved        */
  4. /*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
  5.  
  6. /* 
  7. ** This program computes an adaptive quantized 
  8. ** differential PCM version for speech data
  9. ** reduction.  It processes binary files.  
  10. ** To use with SoundBlaster, first strip the *.VOC header.
  11. ** (see VOC2BIN.C)
  12. */                                                    
  13.  
  14. /*
  15. ** Assume DC start level is mid-range at $80.
  16. ** This is a good assumption for properly biased digitized speech.
  17. ** Define encoding:                
  18. **    2's        ABS         REL
  19. **    -1        $FF        +127    Most positive value
  20. **              *
  21. **    -127    $81     +1        Least positive value
  22. **    -128    $80          0        DC level
  23. **    +127    $7F        -1        Least negative value
  24. **              *
  25. **     0        $00        -128    Most negative value  
  26. **
  27. ** Process signed REL data, since the differential encoding is signed.
  28. ** The sampled data is originally in ABS format with an offset of $80,
  29. ** i.e. the DC level is $80.
  30. **
  31. ** Convert from ABS to REL using signed arithmetic (ABS in 2's form):
  32. ** if (ABS >= $80) REL=ABS-($80) {ie REL=ABS-(-128)}
  33. ** if (ABS <  $80) REL=ABS+($80) {ie REL=ABS+(-128)}  
  34. ** 
  35. ** Alternate conversion: REL=(ABS MOD 256) - 128
  36. **
  37. ** Convert back to ABS during decode before output to DAC.
  38. */
  39.                   
  40. #define BLK_SIZE    64 /* MUST be an even number */ 
  41. #define TRUE        1
  42. #define FALSE        0
  43.  
  44. /*
  45. ** The packed file format is:
  46. ** Qfn    |    d1        Byte1
  47. ** d2    |    d3      Byte2
  48. ** d4    |    d5      Byte3
  49. **  .    |     .
  50. **    .    |     .
  51. **    .    |     .
  52. ** d60    |    d61     Byte31
  53. ** d62    |    d63     Byte32
  54. **
  55. ** The first difference is between the previous value and Data1.
  56. ** The last data (Data_N_; N=BLK_SIZE) is the previous value for
  57. ** the next block.
  58. */
  59.                   
  60. #include <stdio.h>    
  61. #include <io.h> 
  62. #include <stdlib.h>  
  63. #include <math.h> 
  64. #include <string.h>
  65.                     
  66. /* GLOBALS */                    
  67. FILE *infile, *outfile, *testfile;
  68. char sf[255],tf[255];
  69. char report;
  70.                 
  71. void main(argc,argv)
  72. int argc;
  73. char *argv[];
  74. {                            
  75. char response; 
  76. unsigned int blk;
  77. unsigned char i;
  78. signed char byte[256];
  79. signed char prev;
  80. /*unsigned char Qfn;*/
  81.  
  82. /* function definitions */     
  83. signed char ABS2REL(signed char byte); 
  84.  
  85. signed char quant1(signed char byte);
  86. signed char quant2(signed char byte); 
  87. signed char quant3(signed char byte);
  88. signed char quant4(signed char byte);
  89. signed char quant5(signed char byte);
  90. signed char quant6(signed char byte);
  91. signed char quant7(signed char byte);   
  92.  
  93. signed char iq1(signed char byte);
  94. signed char iq2(signed char byte);
  95. signed char iq3(signed char byte);
  96. signed char iq4(signed char byte);
  97. signed char iq5(signed char byte);
  98. signed char iq6(signed char byte);
  99. signed char iq7(signed char byte);
  100.  
  101. void process_blk(signed char byte[],
  102.                  unsigned char size,
  103.                  signed char *prev,
  104.                  FILE *outfile);
  105.  
  106. /* code starts here... */                                      
  107. _wabout("Converts binary files to ADPCM.\nCopyright (C) 1994 Ken Staton.\nAll Rights Reserved.\n");   
  108.                                    
  109.  
  110. if (argc != 3)
  111.   {                                      
  112.   printf("Enter the source file: \n");
  113.   scanf("%s",sf);
  114.   printf("Enter the target file: \n");
  115.   scanf("%s",tf);
  116.   
  117.   report = FALSE;        /* assume no report */
  118.   _wsetexit(_WINEXITNOPERSIST);    /* assume no persistence */                                                          
  119.   printf("Statistics report (y/n)? ");
  120.   scanf("%s",&response);
  121.   if ((response == 'Y') | (response == 'y')) 
  122.    {
  123.     report = TRUE;
  124.     _wsetexit(_WINEXITPERSIST);  /* persist if report generated */
  125.     }
  126.  
  127.   printf("\n\n");
  128.   }
  129. else
  130.   {
  131.   strcpy(sf,argv[1]);
  132.   strcpy(tf,argv[2]);
  133.   report = FALSE;        /* assume no report */
  134.   _wsetexit(_WINEXITNOPERSIST);    /* assume no persistence */                                                          
  135.   }
  136.  
  137. /* OPEN FILES */
  138.    
  139. /* Open for read (will fail if source file does not exist) */
  140. if( (infile  = fopen( sf, "rb" )) == NULL )
  141.   {
  142.   printf( "The file %s was not opened\n",sf );
  143.   exit(-1);
  144.   }
  145. else
  146.   {
  147.   printf( "The file %s was opened\n",sf );
  148.   }
  149.   
  150. /* Open for write */ 
  151. /* test for overwrite! */
  152. if( (testfile = fopen( tf, "rb")) != NULL)
  153.   {
  154.   printf("%s Exists!  OVERWRITE (y/n)?",tf);
  155.   scanf("%s",&response);
  156.   if ((response!='Y') && (response!='y')) exit(-1);
  157.   if( fclose( testfile ) ) printf( "The file %s was not closed\n",tf );  
  158.   printf("\n\n");
  159.   }
  160. /* go ahead and create new file */    
  161. if( (outfile = fopen( tf, "w+b" )) == NULL )
  162.   {
  163.   printf( "The file %s was not opened\n",tf );
  164.   exit(-1);
  165.   }
  166. else
  167.   {
  168.   printf( "The file %s was opened\n",tf );
  169.   }
  170.  
  171. /* PROCESS FILE */
  172.  
  173. blk=1; /* count blocks */
  174. i=1; /* start new block */
  175. (unsigned char)prev=0x00; /* assume DC start value REL value! */
  176. byte[i++]=fgetc(infile);   
  177. while (feof(infile) == 0)
  178.   {
  179. /* get ALL data points in a block before processing.    */
  180. /* the fget is at the end of the while loop,            */
  181. /* with post increment of i, so the test is BLK_SIZE    */
  182. /* which is really an odd number of bytes fetched,        */
  183. /* = BLK_SIZE-1, since indexed from 1 instead of 0.        */
  184.   
  185.   if (i==BLK_SIZE)        
  186.     { /* begin processing block */                          
  187.     if (report) printf("BLK # %4i\n",blk);
  188.     
  189.     process_blk(byte,i,&prev,outfile);
  190.     blk++;                               
  191.     i=1; /* start new block */
  192.     
  193.     
  194.     } /* end processing block */
  195.  
  196.   byte[i++]=fgetc(infile);
  197.   
  198.   } /* end while !EOF */
  199.  
  200. /* partial block to be processed */
  201. if (i>1)            
  202.   {    /* begin processing partial block */                    
  203.   if (report) 
  204.     {
  205.     printf("BLK # %4i\n",blk);  
  206.     printf("Last BLK. %i bytes.\n",i);
  207.     }
  208.   if ((i%2)) i-=1; /* if even, throw away last byte */    
  209.   process_blk(byte,i,&prev,outfile);  
  210.   } /* end processing partial block */
  211.                                       
  212. /* Close streams */
  213. if( fclose( infile ) )
  214.   printf( "The file %s was not closed\n",sf ); 
  215. if( fclose( outfile ) )
  216.   printf( "The file %s was not closed\n",tf );  
  217.  
  218. } /* END MAIN */
  219.  
  220. /*
  221. **
  222. ** SUBROUTINES
  223. **
  224. */
  225.                           
  226. /* Block processing... */
  227.  
  228. void process_blk(signed char byte[],
  229.                  unsigned char size,
  230.                  signed char *prev,
  231.                  FILE *outfile)
  232. {
  233. unsigned char pack;      
  234. unsigned int i;
  235. unsigned char Qfn;
  236. unsigned char diff;
  237. signed char p; 
  238. signed char pp,pn;
  239. int d,m,v;      
  240. double s;
  241.  
  242.     p=*prev;
  243.          
  244.     for (i=1;i<size;i++) /* convert to REL data */
  245.       {
  246.       byte[i]=ABS2REL(byte[i]);
  247.       }       
  248.             
  249.     pp=pn=0;
  250.     m=0;
  251.     for (i=1;i<size;i++) /* peak and mean*/
  252.       {                                                
  253.       d=(byte[i]-p);
  254.       if ((d>=0) && (pp<d)) pp=d;    /* pos peak */
  255.       if ((d<0) && (pn>d)) pn=d;     /* neg peak */
  256.       m=m+d;                           /* mean */
  257.       p=byte[i];
  258.       } 
  259.     m=m/i;
  260.       
  261.     v=0;
  262.     for (i=1;i<size;i++) /* variance */
  263.       {
  264.       d=(byte[i]-m);
  265.       v=v+(d*d);
  266.       }   
  267.     v=v/i;  
  268.     
  269.     s=sqrt(v);   /* standard deviation */
  270.                          
  271.     
  272.     if (report)
  273.       {
  274.       printf("ave = %4i\tvar = %4i\tsd = %4i\n",m,v,(int)s);
  275.       }
  276.     
  277.     /* Select Qfn.  4 bits available, but one reserved    */
  278.     /* for end marker (0000).                            */ 
  279.     /* PEAK VALUE based Qfn selection.                    */ 
  280.     
  281.     if      ((pp < 8) && (pn >=  -8)) Qfn = 1;  
  282.     else if ((pp <16) && (pn >= -16)) Qfn = 2; 
  283.     else if ((pp <32) && (pn >= -32)) Qfn = 3;
  284.     else if ((pp <48) && (pn >= -48)) Qfn = 4;
  285.     else if ((pp <64) && (pn >= -64)) Qfn = 5;
  286.     else if ((pp <96) && (pn >= -96)) Qfn = 6;
  287.     else                              Qfn = 7;
  288.         
  289.     if (report) printf("Qfn = %2i\n\n",Qfn);
  290.     
  291.     byte[0]=Qfn;    /* Put Qfn at index zero.            */
  292.                     /* Will still have an even number,    */
  293.                     /* since there are N-1 differences.    */
  294.         
  295.     for (i=1;i<size;i++) /* quantize in-place */       
  296.       {  
  297.       diff = byte[i] - *prev; 
  298.       if (Qfn==1)
  299.         {
  300.         byte[i]=quant1(diff);
  301.         *prev = *prev+iq1(byte[i]);
  302.         }
  303.       if (Qfn==2)
  304.         {
  305.         byte[i]=quant2(diff);
  306.         *prev = *prev+iq2(byte[i]);
  307.         }                
  308.       if (Qfn==3)
  309.         {
  310.         byte[i]=quant3(diff);
  311.         *prev = *prev+iq3(byte[i]);
  312.         }
  313.       if (Qfn==4)
  314.         {
  315.         byte[i]=quant4(diff);
  316.         *prev = *prev+iq4(byte[i]);
  317.         }
  318.       if (Qfn==5)
  319.         {
  320.         byte[i]=quant5(diff);
  321.         *prev = *prev+iq5(byte[i]);
  322.         }
  323.       if (Qfn==6)
  324.         {
  325.         byte[i]=quant6(diff);
  326.         *prev = *prev+iq6(byte[i]);
  327.         }
  328.       if (Qfn==7)
  329.         {
  330.         byte[i]=quant7(diff);
  331.         *prev = *prev+iq7(byte[i]);
  332.         }                          
  333.       }
  334.                         
  335.     for (i=0;i<size;i+=2)    /* pack & output */
  336.       {
  337.       pack=((byte[i]<<4)|(byte[i+1]&0X0F));                        
  338.       fputc(pack,outfile); 
  339.       }
  340.                                             
  341. } /* end process */                                            
  342.  
  343.  
  344. /*
  345. ** Absolute to relative conversion subroutine
  346. */
  347.                                                 
  348. signed char ABS2REL(signed char byte)
  349. {
  350. signed char REL;
  351.  
  352. if ((unsigned char)byte >= 128)
  353.   REL = byte + 128;
  354. else 
  355.   REL = byte - 128;
  356.  
  357. return(REL);
  358. }   
  359.  
  360.                                                    
  361. /*
  362. **
  363. ** Quantization routines follow.
  364. ** 
  365. ** The result of quatization is always 8 bits signed (signed char),
  366. ** but really interested in 4 bit signed.
  367. **
  368. ** Dec        Bin (2's C)
  369. **  7        0000 0111
  370. **  6        0000 0110
  371. **  5        0000 0101
  372. **  4        0000 0100
  373. **  3        0000 0011
  374. **  2        0000 0010
  375. **  1        0000 0001
  376. **  0        0000 0000
  377. ** -1        1111 1111
  378. ** -2        1111 1110
  379. ** -3        1111 1101
  380. ** -4        1111 1100
  381. ** -5        1111 1011
  382. ** -6        1111 1010
  383. ** -7        1111 1001
  384. ** -8        1111 1000
  385. **
  386. ** Note that the 4 bit 2's complement can be obtained simply by
  387. ** ignoring the upper 4 bits of the 8 bit 2's complement.
  388. **
  389. */
  390.                                                       
  391. /*****************************************************/                                
  392.                                 
  393. signed char quant1(signed char byte)
  394. {               
  395. /* linear */ 
  396. signed char qb;
  397.  
  398. if (byte==0)                     qb=0;
  399. if (byte==1)                     qb=1;                                
  400. if (byte==2)                     qb=2;
  401. if (byte==3)                     qb=3;
  402. if (byte==4)                     qb=4;
  403. if (byte==5)                     qb=5;
  404. if (byte==6)                     qb=6;
  405. if (byte>=7)                     qb=7; 
  406.  
  407. if (byte==-1)                     qb=-1;
  408. if (byte==-2)                     qb=-2;
  409. if (byte==-3)                     qb=-3;
  410. if (byte==-4)                    qb=-4;
  411. if (byte==-5)                     qb=-5;
  412. if (byte==-6)                     qb=-6;
  413. if (byte==-7)                     qb=-7;
  414. if (byte<=-8)                     qb=-8;
  415.  
  416. if (report)
  417.   {
  418.   if (qb==7) 
  419.     printf("QByte max pos (+7) in Q1. Byte = %i\n",byte);
  420.   if (qb==-8)
  421.     printf("QByte max neg (-8) in Q1. Byte = %i\n",byte);
  422.   }
  423. return(qb);
  424. }
  425.  
  426. /*****************************************************/ 
  427.  
  428. signed char quant2(signed char byte)
  429. {
  430. signed char qb;    
  431.  
  432. if (byte==0)                     qb=0;
  433. if (byte==1)                     qb=1;                                
  434. if (byte==2)                     qb=2;
  435. if (byte==3)                     qb=3;
  436. if ((byte>=4) && (byte<6))         qb=4;
  437. if ((byte>=6) && (byte<8))         qb=5;
  438. if ((byte>=8) && (byte<12))     qb=6;
  439. if (byte>=12)                     qb=7; 
  440.  
  441. if (byte==-1)                     qb=-1;
  442. if (byte==-2)                     qb=-2;
  443. if (byte==-3)                     qb=-3;
  444. if (byte==-4)                    qb=-4;
  445. if ((byte<=-5) && (byte>-7))     qb=-5;
  446. if ((byte<=-7) && (byte>-10))     qb=-6;
  447. if ((byte<=-10) && (byte>-13))     qb=-7;
  448. if (byte<=-13)                    qb=-8;
  449.  
  450. if (report)
  451.   {
  452.   if (qb==7) 
  453.     printf("QByte max pos (+12) in Q2. Byte = %i\n",byte);
  454.   if (qb==-8)
  455.     printf("QByte max neg (-13) in Q2. Byte = %i\n",byte);
  456.   }
  457. return(qb);
  458. }                                     
  459.  
  460. signed char quant3(signed char byte)
  461. {
  462. signed char qb; 
  463.  
  464. if (byte==0)                     qb=0;
  465. if (byte==1)                     qb=1;                                
  466. if (byte==2)                     qb=2;
  467. if ((byte>=3) && (byte<6))        qb=3;
  468. if ((byte>=6) && (byte<9))         qb=4;
  469. if ((byte>=9) && (byte<15))     qb=5;
  470. if ((byte>=15) && (byte<24))     qb=6;
  471. if (byte>=24)                     qb=7; 
  472.  
  473. if (byte==-1)                     qb=-1;
  474. if (byte==-2)                     qb=-2;
  475. if ((byte<=-3) && (byte>-5))    qb=-3;
  476. if ((byte<=-5) && (byte>-7))     qb=-4;
  477. if ((byte<=-7) && (byte>-11))     qb=-5;
  478. if ((byte<=-11) && (byte>-17))     qb=-6;
  479. if ((byte<=-17) && (byte>-26))     qb=-7;
  480. if (byte<=-26)                    qb=-8; 
  481.  
  482. if (report)
  483.   {
  484.   if (qb==7) 
  485.     printf("QByte max pos (+24) in Q3. Byte = %i\n",byte);
  486.   if (qb==-8)
  487.     printf("QByte max neg (-26) in Q3. Byte = %i\n",byte);
  488.   }
  489. return(qb);
  490. }
  491.  
  492. signed char quant4(signed char byte)
  493. {
  494. signed char qb;             
  495.  
  496. if (byte==0)                     qb=0;
  497. if (byte==1)                     qb=1;                                
  498. if ((byte>=2) && (byte<4))        qb=2;
  499. if ((byte>=4) && (byte<7))         qb=3;
  500. if ((byte>=7) && (byte<12))     qb=4;
  501. if ((byte>=12) && (byte<21))     qb=5;
  502. if ((byte>=21) && (byte<36))     qb=6;
  503. if (byte>=36)                    qb=7; 
  504.  
  505. if (byte==-1)                     qb=-1;
  506. if (byte==-2)                     qb=-2;
  507. if ((byte<=-3) && (byte>-5))    qb=-3;
  508. if ((byte<=-5)  && (byte>-9))     qb=-4;
  509. if ((byte<=-9)  && (byte>-14))     qb=-5;
  510. if ((byte<=-14) && (byte>-23))     qb=-6;
  511. if ((byte<=-23) && (byte>-38))     qb=-7;
  512. if (byte<=-38)                    qb=-8;
  513.  
  514. if (report)
  515.   {
  516.   if (qb==7) 
  517.     printf("QByte max pos (+36) in Q4. Byte = %i\n",byte);
  518.   if (qb==-8)
  519.     printf("QByte max neg (-38) in Q4. Byte = %i\n",byte);
  520.   }
  521. return(qb);
  522. }
  523.  
  524. /*****************************************************/  
  525.  
  526.  signed char quant5(signed char byte)
  527. {               
  528. /* linear */ 
  529. signed char qb;
  530.  
  531. if (byte==0)                     qb=0;
  532. if (byte==1)                     qb=1;                                
  533. if ((byte>=2) && (byte<4))        qb=2;
  534. if ((byte>=4) && (byte<8))         qb=3;
  535. if ((byte>=8) && (byte<14))     qb=4;
  536. if ((byte>=14) && (byte<26))     qb=5;
  537. if ((byte>=26) && (byte<47))     qb=6;
  538. if (byte>=47)                    qb=7; 
  539.  
  540. if (byte==-1)                     qb=-1;
  541. if ((byte<=-2) && (byte>-4))    qb=-2;
  542. if ((byte<=-4) && (byte>-6))    qb=-3;
  543. if ((byte<=-6) && (byte>-10))     qb=-4;
  544. if ((byte<=-10) && (byte>-17))    qb=-5;
  545. if ((byte<=-17) && (byte>-29))     qb=-6;
  546. if ((byte<=-29) && (byte>-49))     qb=-7;
  547. if (byte<=-49)                    qb=-8;
  548.  
  549. if (report)
  550.   {
  551.   if (qb==7) 
  552.     printf("QByte max pos (+47) in Q5. Byte = %i\n",byte);
  553.   if (qb==-8)
  554.     printf("QByte max neg (-49) in Q5. Byte = %i\n",byte);
  555.   }
  556. return(qb);
  557. }
  558.  
  559. /*****************************************************/ 
  560.  
  561. signed char quant6(signed char byte)
  562. {               
  563. /* linear */ 
  564. signed char qb;
  565.  
  566. if (byte==0)                     qb=0;
  567. if ((byte>=1) && (byte<3))        qb=1;                                
  568. if ((byte>=3) && (byte<5))        qb=2;
  569. if ((byte>=5) && (byte<10))         qb=3;
  570. if ((byte>=10) && (byte<19))     qb=4;
  571. if ((byte>=19) && (byte<36))     qb=5;
  572. if ((byte>=36) && (byte<69))     qb=6;
  573. if (byte>=69)                    qb=7; 
  574.  
  575. if (byte==-1)                     qb=-1;
  576. if ((byte<=-2) && (byte>-4))    qb=-2;
  577. if ((byte<=-4) && (byte>-7))    qb=-3;
  578. if ((byte<=-7) && (byte>-13))     qb=-4;
  579. if ((byte<=-13) && (byte>-23))    qb=-5;
  580. if ((byte<=-23) && (byte>-41))     qb=-6;
  581. if ((byte<=-41) && (byte>-72))     qb=-7;
  582. if (byte<=-72)                    qb=-8;
  583.  
  584. if (report)
  585.   {
  586.   if (qb==7) 
  587.     printf("QByte max pos (+69) in Q6. Byte = %i\n",byte);
  588.   if (qb==-8)
  589.     printf("QByte max neg (-72) in Q6. Byte = %i\n",byte);
  590.   }
  591. return(qb);
  592. }
  593.  
  594. /*****************************************************/ 
  595.  
  596. signed char quant7(signed char byte)
  597. {               
  598. /* linear */ 
  599. signed char qb;
  600.  
  601. if (byte==0)                     qb=0;
  602. if ((byte>=1) && (byte<3))        qb=1;                                
  603. if ((byte>=3) && (byte<6))        qb=2;
  604. if ((byte>=6) && (byte<11))         qb=3;
  605. if ((byte>=11) && (byte<23))     qb=4;
  606. if ((byte>=23) && (byte<45))     qb=5;
  607. if ((byte>=45) && (byte<90))     qb=6;
  608. if (byte>=90)                    qb=7; 
  609.  
  610. if (byte==-1)                     qb=-1;
  611. if ((byte<=-2) && (byte>-5))    qb=-2;
  612. if ((byte<=-5) && (byte>-8))    qb=-3;
  613. if ((byte<=-8) && (byte>-15))     qb=-4;
  614. if ((byte<=-15) && (byte>-28))    qb=-5;
  615. if ((byte<=-28) && (byte>-52))     qb=-6;
  616. if ((byte<=-52) && (byte>-95))     qb=-7;
  617. if (byte<=-95)                    qb=-8;
  618.  
  619. if (report)
  620.   {
  621.   if (qb==7) 
  622.     printf("QByte max pos (+90) in Q7. Byte = %i\n",byte);
  623.   if (qb==-8)
  624.     printf("QByte max neg (-95) in Q7. Byte = %i\n",byte);
  625.   }
  626. return(qb);
  627. }
  628.  
  629. /*****************************************************/ 
  630.  
  631. /*****************************************************/  
  632.                                
  633. /* input to these inverse quantizers is 8 bits. */
  634. /* select minimum value in ranges... */
  635.                                
  636.                                 
  637. signed char iq1(signed char byte)
  638. {                 
  639. signed char qb;
  640. if (report) if (!((byte<8)&&(byte>-9))) printf("ERROR: Byte range. Byte = %i\n",byte);
  641.  
  642. /* linear */
  643. if (byte==0)                     qb=0;
  644. if (byte==1)                     qb=1;                                
  645. if (byte==2)                     qb=2;
  646. if (byte==3)                     qb=3;
  647. if (byte==4)                     qb=4;
  648. if (byte==5)                    qb=5;
  649. if (byte==6)                    qb=6;
  650. if (byte==7)                    qb=7;
  651.  
  652. if (byte==-1)                     qb=-1;
  653. if (byte==-2)                     qb=-2;
  654. if (byte==-3)                     qb=-3;
  655. if (byte==-4)                    qb=-4;
  656. if (byte==-5)                     qb=-5;
  657. if (byte==-6)                    qb=-6;
  658. if (byte==-7)                    qb=-7;
  659. if (byte==-8)                    qb=-8;
  660. return(qb);
  661. }
  662.  
  663. /*****************************************************/ 
  664.  
  665. signed char iq2(signed char byte)
  666. {
  667. signed char qb;
  668. if (report) if (!((byte<8)&&(byte>-9))) printf("ERROR: Byte range. Byte = %i\n",byte);
  669.  
  670. if (byte==0)                     qb=0;
  671. if (byte==1)                     qb=1;                                
  672. if (byte==2)                     qb=2;
  673. if (byte==3)                     qb=3;
  674. if (byte==4)                     qb=4;
  675. if (byte==5)                    qb=6;
  676. if (byte==6)                    qb=8;
  677. if (byte==7)                    qb=12;
  678.  
  679. if (byte==-1)                     qb=-1;
  680. if (byte==-2)                     qb=-2;
  681. if (byte==-3)                     qb=-3;
  682. if (byte==-4)                    qb=-4;
  683. if (byte==-5)                     qb=-5;
  684. if (byte==-6)                    qb=-7;
  685. if (byte==-7)                    qb=-10;
  686. if (byte==-8)                    qb=-13;
  687.  
  688. return(qb);
  689. }                                     
  690.  
  691. signed char iq3(signed char byte)
  692. {
  693. signed char qb;
  694. if (report) if (!((byte<8)&&(byte>-9))) printf("ERROR: Byte range. Byte = %i\n",byte);
  695.  
  696. if (byte==0)                     qb=0;
  697. if (byte==1)                     qb=1;                                
  698. if (byte==2)                     qb=2;
  699. if (byte==3)                    qb=3;
  700. if (byte==4)                    qb=6;
  701. if (byte==5)                    qb=9;
  702. if (byte==6)                    qb=15;
  703. if (byte==7)                    qb=24;
  704.  
  705. if (byte==-1)                     qb=-1;
  706. if (byte==-2)                     qb=-2;
  707. if (byte==-3)                     qb=-3;
  708. if (byte==-4)                    qb=-5;
  709. if (byte==-5)                    qb=-7;
  710. if (byte==-6)                    qb=-11;
  711. if (byte==-7)                    qb=-17;
  712. if (byte==-8)                    qb=-26;
  713.  
  714. return(qb);
  715. }
  716.  
  717. signed char iq4(signed char byte)
  718. {
  719. signed char qb;
  720. if (report) if (!((byte<8)&&(byte>-9))) printf("ERROR: Byte range. Byte = %i\n",byte);
  721.  
  722. if (byte==0)                     qb=0;
  723. if (byte==1)                     qb=1;                                
  724. if (byte==2)                     qb=2;
  725. if (byte==3)                    qb=4;
  726. if (byte==4)                    qb=7;
  727. if (byte==5)                    qb=12;
  728. if (byte==6)                    qb=21;
  729. if (byte==7)                    qb=36;
  730.                                     
  731.  
  732. if (byte==-1)                     qb=-1;
  733. if (byte==-2)                     qb=-2;
  734. if (byte==-3)                     qb=-3;
  735. if (byte==-4)                    qb=-5;
  736. if (byte==-5)                    qb=-9;
  737. if (byte==-6)                    qb=-14;
  738. if (byte==-7)                    qb=-23;
  739. if (byte==-8)                    qb=-38;
  740.                                     
  741.  
  742. return(qb);
  743. }
  744.  
  745. /*****************************************************/   
  746.  
  747. signed char iq5(signed char byte)
  748. {                 
  749. signed char qb;
  750. if (report) if (!((byte<8)&&(byte>-9))) printf("ERROR: Byte range. Byte = %i\n",byte);
  751.  
  752. if (byte==0)                     qb=0;
  753. if (byte==1)                     qb=1;                                
  754. if (byte==2)                     qb=2;
  755. if (byte==3)                     qb=4;
  756. if (byte==4)                     qb=8;
  757. if (byte==5)                    qb=14;
  758. if (byte==6)                    qb=26;
  759. if (byte==7)                    qb=47;
  760.  
  761. if (byte==-1)                     qb=-1;
  762. if (byte==-2)                     qb=-2;
  763. if (byte==-3)                     qb=-4;
  764. if (byte==-4)                    qb=-6;
  765. if (byte==-5)                     qb=-10;
  766. if (byte==-6)                    qb=-17;
  767. if (byte==-7)                    qb=-29;
  768. if (byte==-8)                    qb=-49;
  769. return(qb);
  770. }
  771.  
  772. /*****************************************************/ 
  773.  
  774. signed char iq6(signed char byte)
  775. {                 
  776. signed char qb;
  777. if (report) if (!((byte<8)&&(byte>-9))) printf("ERROR: Byte range. Byte = %i\n",byte);
  778.  
  779. if (byte==0)                     qb=0;
  780. if (byte==1)                     qb=1;                                
  781. if (byte==2)                     qb=3;
  782. if (byte==3)                     qb=5;
  783. if (byte==4)                     qb=10;
  784. if (byte==5)                    qb=19;
  785. if (byte==6)                    qb=36;
  786. if (byte==7)                    qb=69;
  787.  
  788. if (byte==-1)                     qb=-1;
  789. if (byte==-2)                     qb=-2;
  790. if (byte==-3)                     qb=-4;
  791. if (byte==-4)                    qb=-7;
  792. if (byte==-5)                     qb=-13;
  793. if (byte==-6)                    qb=-23;
  794. if (byte==-7)                    qb=-41;
  795. if (byte==-8)                    qb=-72;
  796. return(qb);
  797. }
  798.  
  799. /*****************************************************/ 
  800.  
  801. signed char iq7(signed char byte)
  802. {                 
  803. signed char qb;
  804. if (report) if (!((byte<8)&&(byte>-9))) printf("ERROR: Byte range. Byte = %i\n",byte);
  805.  
  806. /* linear */
  807. if (byte==0)                     qb=0;
  808. if (byte==1)                     qb=1;                                
  809. if (byte==2)                     qb=3;
  810. if (byte==3)                     qb=6;
  811. if (byte==4)                     qb=11;
  812. if (byte==5)                    qb=23;
  813. if (byte==6)                    qb=45;
  814. if (byte==7)                    qb=90;
  815.  
  816. if (byte==-1)                     qb=-1;
  817. if (byte==-2)                     qb=-2;
  818. if (byte==-3)                     qb=-5;
  819. if (byte==-4)                    qb=-8;
  820. if (byte==-5)                     qb=-15;
  821. if (byte==-6)                    qb=-28;
  822. if (byte==-7)                    qb=-52;
  823. if (byte==-8)                    qb=-95;
  824. return(qb);
  825. }
  826.  
  827. /*****************************************************/ 
  828.